package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoService facilityInfoService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private LabelInfoService labelInfoService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private LeaseTermService leaseTermService;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> itemVoIPage, RoomQueryVo queryVo) {
        IPage<RoomItemVo> iPage = baseMapper.selectPageItem(itemVoIPage, queryVo);
        return iPage;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        // 房间信息
        RoomInfo roomInfo = this.getById(id);
        if (roomInfo == null) {
            // 房间是否被删除
            RoomDetailVo roomDetailVo1 = new RoomDetailVo();
            roomDetailVo1.setIsDelete(true);
            return roomDetailVo1;
        }

        // 增加浏览记录
        browsingHistoryService
                .saveOrUpdate(new BrowsingHistory(LoginUserContext.getLoginUser().getUserId(),  id,new Date()));

        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        // 所属公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        // 图片列表
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
        graphInfoQueryWrapper.eq("item_id", id);
        List<GraphInfo> graphInfos = graphInfoService.list(graphInfoQueryWrapper);
        List<GraphVo> graphVoList = graphInfos.stream()
                .map(graphInfo -> {
                    GraphVo graphVo = new GraphVo();
                    graphVo.setName(graphInfo.getName());
                    graphVo.setUrl(graphInfo.getUrl());
                    return graphVo;
                })
                .collect(Collectors.toList());
        roomDetailVo.setGraphVoList(graphVoList);
        // 属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        // 配套信息列表
        QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
        roomFacilityQueryWrapper.eq("room_id", id);
        List<RoomFacility> list = roomFacilityService.list(roomFacilityQueryWrapper);
        List<FacilityInfo> facilityInfoList = new ArrayList<>();
        for (RoomFacility roomFacility : list) {
            Long facilityId = roomFacility.getFacilityId();
            FacilityInfo facilityInfo = facilityInfoService.getById(facilityId);
            facilityInfoList.add(facilityInfo);
        }
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        // 标签信息列表
        QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
        roomLabelQueryWrapper.eq("room_id", id);
        List<RoomLabel> roomLabelList = roomLabelService.list(roomLabelQueryWrapper);
        List<LabelInfo> labelInfoList = new ArrayList<>();
        for (RoomLabel roomLabel : roomLabelList) {
            Long labelId = roomLabel.getLabelId();
            LabelInfo labelInfo = labelInfoService.getById(labelId);
            labelInfoList.add(labelInfo);
        }
        roomDetailVo.setLabelInfoList(labelInfoList);

        // 支付方式列表
        QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq("room_id", id);
        List<RoomPaymentType> roomPaymentTypeList = roomPaymentTypeService.list(roomPaymentTypeQueryWrapper);
        List<PaymentType> paymentTypeList = new ArrayList<>();
        for (RoomPaymentType roomPaymentType : roomPaymentTypeList) {
            Long paymentTypeId = roomPaymentType.getPaymentTypeId();
            PaymentType paymentType = paymentTypeService.getById(paymentTypeId);
            paymentTypeList.add(paymentType);
        }
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        // 杂费列表
        Long apartmentId = roomInfo.getApartmentId();
        List<FeeValueVo> feeValueVos = feeValueMapper.selectFeeValueVoByApartmentId(apartmentId);
        roomDetailVo.setFeeValueVoList(feeValueVos);

        // 租期列表
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
        roomLeaseTermQueryWrapper.eq("room_id", id);
        List<RoomLeaseTerm> roomLeaseTermList = roomLeaseTermService.list(roomLeaseTermQueryWrapper);
        List<LeaseTerm> leaseTermList = new ArrayList<>();
        for (RoomLeaseTerm roomLeaseTerm : roomLeaseTermList) {
            Long leaseTermId = roomLeaseTerm.getLeaseTermId();
            LeaseTerm leaseTerm = leaseTermService.getById(leaseTermId);
            leaseTermList.add(leaseTerm);
        }
        roomDetailVo.setLeaseTermList(leaseTermList);


        // 房间是否已入住
        QueryWrapper<LeaseAgreement> leaseAgreementQueryWrapper = new QueryWrapper<>();
        leaseAgreementQueryWrapper.eq("room_id", id);
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectOne(leaseAgreementQueryWrapper);
        if(leaseAgreement != null
                && (leaseAgreement.getStatus() == LeaseStatus.SIGNING
                || leaseAgreement.getStatus() == LeaseStatus.SIGNED
                || leaseAgreement.getStatus() == LeaseStatus.EXPIRED
                || leaseAgreement.getStatus() == LeaseStatus.WITHDRAWING
                || leaseAgreement.getStatus() == LeaseStatus.RENEWING)){
            roomDetailVo.setIsCheckIn( true);
        }else{
            roomDetailVo.setIsCheckIn(false);
        }

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> itemVoIPage, Long apartmentId) {
        IPage<RoomItemVo> iPage = baseMapper.selectPageItemByApartmentId(itemVoIPage, apartmentId);
        return iPage;
    }
}




